import { Component } from '@angular/core';
import { IonicPage} from 'ionic-angular';
import { HttphandleProvider } from '../../providers/httphandle/httphandle';
import { LocalstorageProvider } from '../../providers/localstorage/localstorage';
import { Router } from '@angular/router';
import { NativePageTransitions } from '@ionic-native/native-page-transitions';
import { Tools } from '../../tools/tools';
import { Network } from '@ionic-native/network';
/**
 * Generated class for the ProductOrderPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-product-order',
  templateUrl: 'product-order.html',
})
export class ProductOrderPage extends Tools{
  private isTrue = false;
  private datalist;
  private item;
  private isShow;
  private isPro = true;
  private isW = true;
  private isEnd = true;
  private info = "";
  private default ;
  private n = 0;
  private startLine ;
  private imgsrc:string=""
  constructor(
    private http: HttphandleProvider,
    private loc: LocalstorageProvider,
    protected route: Router,
    protected nativePageTransitions: NativePageTransitions,
    protected network: Network
  ) {
    super(route, nativePageTransitions, network)
  }
  ngOnInit() {
    this.getmsg()
  }
  back(){
    this.goback()
  }
  changeline() {
    this.getmsg()
    this.isShow = false;
    this.rest()
    var oWhole = document.getElementById("whole");
    oWhole.className = "active";
    this.isPro = true;
    this.isW = true;
    this.isEnd = true;
    console.log(this.item)
  }
  clickWhole($event) {
    this.rest()
    $event.target.className = "active"
    this.isPro = true;
    this.isW = true;
    this.isEnd = true;
  }
  clickProduction($event) {
    this.rest()
    $event.target.className = "active";
    this.isPro = true;
    this.isW = false;
    this.isEnd = false;
  }
  clickToBeProduced($event) {
    this.rest()
    $event.target.className = "active"
    this.isPro = false;
    this.isW = true;
    this.isEnd = false;
  }
  clickCompletion($event) {
    this.rest()
    $event.target.className = "active"
    this.isPro = false;
    this.isW = false;
    this.isEnd = true;
  }
  rest() {
    var oWhole = document.getElementById("whole");
    var oCompletion = document.getElementById("completion");
    var oToBeProduced = document.getElementById("toBeProduced");
    var oProduction = document.getElementById("production");
    oWhole.className = "";
    oCompletion.className = "";
    oToBeProduced.className = "";
    oProduction.className = "";
  }
  getmsg(){
    this.http.get("/app/rest/v2/services/yes_ViewOrderListService/getOrderByLineLeader").then((res)=>{
      if(res.success){
        if (res.success.length == 0) {
          this.info = '您尚未有任何产线';
          this.isTrue = false;
          this.imgsrc = "./assets/imgs/errorinfo.png"
        }else{
          this.datalist = res.success;
          this.isTrue = true;
          this.info = "";
          this.item = res.success[this.n]
          this.default = res.success[this.n].productionLine
        }
        //console.log(this.default)
        //console.log("success")
      }else{
        this.info = res.error;
        this.isTrue = false;
        this.imgsrc = "./assets/imgs/errorinfo.png"
        console.log("error")
      }

    }).catch(err => {
      //重新获取
      if (err.status == 401) {
        this.http.getToken((res) => {
          this.loc.set('token', res.json())
          this.getmsg()
        })
      } else {
        console.log('请求缺陷存在未知错误', err)
        this.info = err.errMsg || err;
        this.isTrue = false;
        this.imgsrc = "./assets/imgs/errorinfo.png"
        }
    })
  }
  //下拉刷新
  doRefresh(refresher){
    this.http.get("/app/rest/v2/services/yes_ViewOrderListService/getOrderByLineLeader").then((res) => {
      console.log(res)
      if (res.success) {
        if (res.success.length == 0) {
          this.info = '您尚未有任何产线';
          this.isTrue = false;
          this.imgsrc = "./assets/imgs/errorinfo.png"
        } else {
          this.datalist = res.success;
          this.isTrue = true;
          this.info = "";
          this.item = res.success[this.n]
          this.default = res.success[this.n].productionLine
        }
      } else {
        this.info = res.error;
        this.isTrue = false;
        this.imgsrc = "./assets/imgs/errorinfo.png"
        console.log("error")
      }
      refresher.complete();
    }).catch(err => {
      //重新获取
      if (err.status == 401) {
        this.http.getToken((res) => {
          this.loc.set('token', res.json())
          this.getmsg()
        })
      } else {
        console.log('请求缺陷存在未知错误', err)
        this.info = err.errMsg || err;
        this.isTrue = false;
        this.imgsrc = "./assets/imgs/errorinfo.png"
      }
      refresher.complete();
    })

  }
  ionViewDidLoad() {
    console.log('ionViewDidLoad ProductOrderPage');
  }

}
